package com.timoon.component.panel.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.timoon.component.panel.base.BaseController;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

@Controller
@RequestMapping(value="mc")
public class MainController extends BaseController{

	private static Map<String,Object> controllerMap =new HashMap<String,Object>();
	
	@ResponseBody
	@RequestMapping(value="/onAction/{mapping}/{methodName}" ,method=RequestMethod.GET)
	public void onAction(@PathVariable String mapping,@PathVariable String methodName,HttpServletRequest request, HttpServletResponse response){
		
		Object controller=MainController.controllerMap.get(mapping);
		if(controller==null){
			throw new RuntimeException("请求的mapping不存在");
		}
		
		Class<?> clazz=controller.getClass();
		try {
			Method method=clazz.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
			try {
				method.invoke(controller, request,response);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void registController(String mapping,Object controller){
		MainController.controllerMap.put(mapping, controller);
	}
}
